package com.zygk.qybapp.dao;

import android.content.Context;
import com.yanzhenjie.nohttp.RequestMethod;
import com.yanzhenjie.nohttp.rest.Response;
import com.yanzhenjie.nohttp.rest.SimpleResponseListener;
import com.yanzhenjie.nohttp.rest.StringRequest;
import com.zygk.library.base.BaseActivity;
import com.zygk.library.util.CallServer;
import com.zygk.library.util.JsonUtil;
import com.zygk.library.util.ToastUtil;
import com.zygk.qybapp.config.Urls;
import com.zygk.qybapp.model.apimodel.APIM_AppInfo;
import com.zygk.qybapp.util.HttpRequest;
import org.json.JSONException;
import org.json.JSONObject;

public class CommonLogic {
  /**
   * 获取系统最新版本
   * mContext 上下文
   */
  public static void check_app_version_code(Context context, String versionCode,
      final HttpRequest.HttpCallback callback) {
    StringRequest request =
        new StringRequest(Urls.URL + Urls.Check_app_version_code, RequestMethod.POST);
    JSONObject object = new JSONObject();
    try {
      object.put("appType", "1")
          .put("nowVersionCode", versionCode);
    } catch (JSONException e) {
      e.printStackTrace();
    }
    request.setDefineRequestBodyForJson(object);

    SimpleResponseListener<String> listener = new SimpleResponseListener<String>() {
      @Override public void onStart(int what) {
        super.onStart(what);
        callback.onStart();
      }

      @Override public void onSucceed(int what, Response<String> response) {
        super.onSucceed(what, response);
        APIM_AppInfo result = JsonUtil.jsonToObject(response.get(), APIM_AppInfo.class);

        if (result == null) return;

        if (result.getStatus() == 1) {
          callback.onSucceed(result);
        } else {
          ToastUtil.showMessage(context, result.getInfo());
        }
      }

      @Override public void onFailed(int what, Response<String> response) {
        super.onFailed(what, response);
        callback.onFailed();
      }

      @Override public void onFinish(int what) {
        super.onFinish(what);
        callback.onFinish();
      }
    };
    CallServer.getInstance().request(0, request, listener);
  }
}
